package com.cloud.drore.eboss.operation.service.impl;


import com.cloud.drore.eboos.common.util.LocalDateAndDateTransformUtil;
import com.cloud.drore.eboss.operation.entity.vo.SupplierSettlementInfoVo;
import com.cloud.drore.eboss.supplier.mapper.SupplierSettlementAutoInfoMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;


import javax.annotation.Resource;

import java.time.LocalDate;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;


/**
 * 浙江卓锐科技股份有限公司 版权所有 © Copyright 2018<br>
 *
 * @Description: 财务模块接口<br>
 * @Project: eboos <br>
 * @CreateDate: Created in 2018/01/22 11:23 <br>
 * @Author: <a href="836327318@qq.com">夏凯</a>
 */
@Component
@Slf4j
public class AutoSettlementImpl {
    @Autowired
    TimeTaskServiceImpl timeTaskServiceImpl;
    @Autowired
    FinaceSettlementHistoryServiceImpl finaceSettlementHistoryServiceImpl;

    @Resource
    SupplierSettlementAutoInfoMapper supplierSettlementAutoInfoMapper;
    //@Scheduled(cron = "0 0 2 * * ?")
    //@ZchainScheduled(cron = "0/10 * * * * ? ")

    /**
     * @param
     * @ClassName: SaveLoggerAdBugTimer
     * @Return: void
     * @Decription: 每天2点常驻任务进行供应商的结算任务扫描，符合条件的进行结算
     * @CreateDate: Created in 2018/4/3 0003 上午 9:19
     * @Author: <a href="410508961@qq.com">夏凯</a>
     * @Modify:
     */
    @Scheduled(cron = "0 0 0 * * ? ")
    @Transactional
    public void supplierAutoSettlement() {
        //查询满足条件可以进行结算的供应商
        List<SupplierSettlementInfoVo> supplierSettlementInfoVos = supplierSettlementAutoInfoMapper.querySupplierInfoList();
        Date date = new Date();
        supplierSettlementInfoVos.stream().map(supplierSettlementInfoVo -> {
            supplierSettlementInfoVo.setSettlementDate(supplierSettlementInfoVo.getSettlementDate());
            return supplierSettlementInfoVo;
        }).collect(Collectors.toList());

        try {
            //异常结算
            if (supplierSettlementInfoVos.get(0).getSettlementState()==1) {
                finaceSettlementHistoryServiceImpl.AutoinitFinaceSettlementHistory(supplierSettlementInfoVos);
            } else {
                //正常结算
                if (LocalDateAndDateTransformUtil.dateToLocalDate(supplierSettlementInfoVos.get(0).getSettlementDate()).equals(LocalDate.now())) {
                    finaceSettlementHistoryServiceImpl.AutoinitFinaceSettlementHistory(supplierSettlementInfoVos);
                }
            }
        } catch (Exception e) {
            timeTaskServiceImpl.updateSettlementFlag();
        }
    }

    /**
     * @param supplierSettlementInfo
     * @ClassName: SaveLoggerAdBugTimer
     * @Return: void
     * @Decription: 供应商任务启动的时候偶去结算
     * @CreateDate: Created in 2018/4/3 0003 下午 1:55
     * @Author: <a href="410508961@qq.com">夏凯</a>
     * @Modify:
     */
/*    @Transactional
    public void supplierSettlement(SupplierSettlementInfoVo supplierSettlementInfo) {
        try {
            //异常结算
            if (supplierSettlementInfo.getSettlementState() == 1) {
                finaceSettlementHistoryServiceImpl.initFinaceSettlementHistory(supplierSettlementInfo);
                log.info("供应商id：" + supplierSettlementInfo.getSupplierId() + " 异常结算成功！");
            } else {
                //正常结算
                if (LocalDateAndDateTransformUtil.dateToLocalDate(supplierSettlementInfo.getSettlementDate()).equals(LocalDate.now())) {
                    finaceSettlementHistoryServiceImpl.initFinaceSettlementHistory(supplierSettlementInfo);
                    log.info("供应商id：" + supplierSettlementInfo.getSupplierId() + " 正常结算成功！");
                }
            }

        } catch (Exception e) {
            log.info("供应商id：" + supplierSettlementInfo.getSupplierId() + " 结算出现异常！");
            timeTaskServiceImpl.updateSupplierInfo(supplierSettlementInfo.getSupplierId());
            log.info("供应商id：" + supplierSettlementInfo.getSupplierId() + " 将正常状态改为异常！");
        }*/


}